package com.kmg.scart.dao.impl;

import java.io.Serializable;

import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.kmg.scart.dao.interfaces.ApplicationRepository;
import com.kmg.scart.model.ApplicationEntity;

@Transactional
public class ApplicationRepositoryImpl<T extends ApplicationEntity,PK extends Serializable> implements ApplicationRepository<T, PK> {

	private SessionFactory sessionFactory;
	
	private Class<T> type;
	
	public ApplicationRepositoryImpl(Class<T> type){
		this.type=type;
	}
	
	@Override
	public void create(T data) {
		sessionFactory.getCurrentSession().save(data);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public T find(Serializable key) {
		return (T)sessionFactory.getCurrentSession().get(type, key);
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void update(T data){
		this.sessionFactory.getCurrentSession().saveOrUpdate(data);
	}

	@Override
	public void evict(T data) {
		this.sessionFactory.getCurrentSession().flush();
		this.sessionFactory.getCurrentSession().evict(data);
		
	}
	
}
